package com.usian.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.model.admin.dtos.ChannelDto;
import com.usian.admin.model.admin.pojos.AdChannel;
import com.usian.admin.model.common.dtos.PageResponseResult;
import com.usian.admin.model.common.dtos.ResponseResult;
import com.usian.admin.service.AdChannelService;
import com.usian.base.BaseServiceImpl;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends BaseServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        if (dto==null)return paramError;
        Page page=new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }
        IPage result=page(page,lambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) result.getTotal());
        pageResponseResult.setData(result.getRecords());
        return pageResponseResult;
    }
    @Override
    public ResponseResult insert(AdChannel channel) {
        //1.检查参数
        if(channel==null|| StringUtils.isBlank(channel.getName()))return paramError;
        channel.setCreatedTime(new Date());
        save(channel);
        return okResult;
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        if(adChannel==null|| StringUtils.isBlank(adChannel.getName()))return paramError;
        adChannel.setCreatedTime(new Date());
        updateById(adChannel);
        return okResult;
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        AdChannel adChannel = getById(id);
        if(adChannel==null|| StringUtils.isBlank(adChannel.getName()))return paramError;
        removeById(id);
        return okResult;
    }
}
